﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.WindowsMobile.Samples.Location;
using Mirkuz.Mapping;

namespace Mirkuz.TrailCompanion
{
    public class TrainManager
    {
        double _remainingdistance=0;
        double _totaldistance = 0;
        double _maxdistance=0;
        TimeSpan _remainingtime=TimeSpan.Zero;
        TimeSpan _elapsedtime = TimeSpan.Zero;        
        TimeSpan _maxrunningtime = TimeSpan.Zero;

        DateTime _starttime = DateTime.Now;
        
        MapPoint _previousposition = null;        
        MapPoint _currentposition = null;

        public MapTrack Track;

        Boolean _running=false;

        PositionTrackDataSet _positiondata = new PositionTrackDataSet();

        public TrainManager()
        {
        }

        public void StartTraining(MapPoint StartPosition)
        {
            if (StartPosition == null)
                throw new NullReferenceException("Invalid Start Position");

            Track = new MapTrack();
            Track.AddStartPoint(StartPosition);

            _running = true;
            _starttime = DateTime.Now;
            
            _previousposition = StartPosition;            
            
            _remainingtime = _maxrunningtime;
            _elapsedtime = TimeSpan.Zero;
            _remainingdistance = _maxdistance;
            _totaldistance = 0;
                               
        }

        public void StopTraining()
        {
            _running = false;

            Track.AddFinishPoint(_currentposition);
        }

        public bool IsRunning
        {
            get { return _running; }
        }


        /// <summary>
        /// Aggiorna i dati dell'allenamento con una nuova posizione 
        /// </summary>
        /// <param name="Position"></param>
        public void UpdatePosition(MapPoint Position)
        {
            _currentposition = Position;

            if (!_running)
                return;

            if (_previousposition != null && _currentposition != null)
            {
                _totaldistance += _currentposition.SurfaceDistanceMeters(_previousposition);
                _remainingdistance = _maxdistance - _totaldistance;
            }

            if (_currentposition != null)
            {
                //traccia per mappa
                Track.AddPoint(_currentposition);
             
                //recordset registrazioni
                _positiondata.TrackRecord.AddTrackRecordRow(DateTime.Now, _currentposition.Latitude, _currentposition.Longitude, _totaldistance, _currentposition.Speed * 1.852, _currentposition.Altitude);
                _previousposition = _currentposition;
            }
        }

        /// <summary>
        /// aggiorna i dati dell'allenamento
        /// </summary>
        public void Tick()
        {
            if (!_running)
                return;

            //calcolo del tempo trascorso dallo start
            _elapsedtime = DateTime.Now.Subtract(_starttime);
            _remainingtime = _maxrunningtime.Subtract(_elapsedtime);

        }
        public double RunningDistance
        {
            get { return _totaldistance; }
        }
        public double RemainingDistance
        {
            get { return _remainingdistance; }
        }
        public TimeSpan RemainingTime
        {
            get { return _remainingtime; }
        }
        public TimeSpan RunningTime
        {
            get { return _elapsedtime; }
        }
        public TimeSpan MaxRunningTime
        {
            get { return _maxrunningtime; }
            set
            {
                if (!_running)
                    _maxrunningtime = value;
                else
                    throw new Exception("Cannot change objectives while training");
            }
        }
        public double MaxDistance
        {
            get { return _maxdistance; }
            set
            {
                if (!_running)
                    _maxdistance= value;
                else
                    throw new Exception("Cannot change objectives while training");
            }
        }

        public PositionTrackDataSet TrainDataSet
        {
            get { return _positiondata; }
        }
    }
}
